package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

import java.util.List;

/**
 * @Author: bowen
 * @Date: 2019/6/28 16:08
 * @Description:套餐
 */
@RestController
@RequestMapping("/setmeal")
public class SetmealController {

	@Autowired
	JedisPool jedisPool;

	@Reference
	SetmealService setmealService;

    /**
     *  @描述：获取套餐列表
     */
	@RequestMapping("/getSetmeal")
    public Result getSetmeal() {
		try {
			List<Setmeal> setmealList = setmealService.getSetmeal();
			return new Result(true,MessageConstant.QUERY_SETMEAL_SUCCESS,setmealList );
		} catch(Exception e){
			e.printStackTrace();
			return new Result(false, MessageConstant.QUERY_SETMEAL_FAIL);

		}
    }
    /**
     *  @描述：根据id查询套餐详情
     */
    @RequestMapping("/findById")
    public Result findById(Integer id) {
		String setmealDetail = jedisPool.getResource().get("setmealId"+id);
		if (!ObjectUtils.isEmpty(setmealDetail)){
			return new Result(true,MessageConstant.QUERY_SETMEAL_SUCCESS, JSON.parse(setmealDetail));
		}
		try {
			Setmeal setmeal= setmealService.findById(id);
			String setmealJson= JSON.toJSONString(setmeal);
			jedisPool.getResource().set("setmealId"+id,setmealJson);
			return new Result(true,MessageConstant.QUERY_SETMEAL_SUCCESS,setmeal );
		} catch(Exception e){
			e.printStackTrace();
			return new Result(false,MessageConstant.QUERY_SETMEAL_FAIL);
		}
    }

}
